package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-22
 */
@RestController
@CrossOrigin
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    //生成二维码支付接口
    //参数是订单id
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        //返回二维码和其他信息
        Map map = payLogService.createNative(orderNo);
        System.out.println("******返回二维码map集合："+map);
        return R.ok().data(map);
    }


    @GetMapping("queryPayStatus/{orderNo}")
    public R quereyPayStatus(@PathVariable String orderNo) {
        //根据订单id查询订单支付状态
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("*****返回订单状态map集合："+map);
        if (map == null) {
            return R.error().message("支付失败");
        }
        //如果返回的map不为空，通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {
            //添加记录到支付表，更新订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }
}

